package com.fjwt.gz.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.Constants;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: Will
 * @Date: 2024-05-18
 * @Version: 1.0
 */
public class Utils {

    /**
     * 生成批次号
     */
    public static String generateBathNo() {
        String currentDate = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN);
        String redisDate = RedisUtil.getString(Constants.REDIS_BATCH_NO.CURRENT_DATE);
        if (!currentDate.equals(redisDate)) {
            RedisUtil.setString(Constants.REDIS_BATCH_NO.CURRENT_DATE, currentDate);
            RedisUtil.set(Constants.REDIS_BATCH_NO.BATCH_NO, 0);
        }
        Long returnStep = RedisUtil.increment(Constants.REDIS_BATCH_NO.BATCH_NO, Constants.REDIS_BATCH_NO.STEP);
        if (returnStep > 999) {
            return String.format("%s%s", currentDate, returnStep);
        } else if (returnStep > 99) {
            return String.format("%s0%s", currentDate, returnStep);
        } else if (returnStep > 9) {
            return String.format("%s00%s", currentDate, returnStep);
        } else {
            return String.format("%s000%s", currentDate, returnStep);
        }
    }

    /**
     * 生成渠道码后接编号
     */
    public static String generateChannelCode() {
        String currentDate = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN);
        String redisDate = RedisUtil.getString(Constants.REDIS_CHANNEL_CODE.CURRENT_DATE);
        if (!currentDate.equals(redisDate)) {
            RedisUtil.setString(Constants.REDIS_CHANNEL_CODE.CURRENT_DATE, currentDate);
            RedisUtil.set(Constants.REDIS_CHANNEL_CODE.CHANNEL_CODE, 0);
        }
        Long returnStep = RedisUtil.increment(Constants.REDIS_CHANNEL_CODE.CHANNEL_CODE, Constants.REDIS_BATCH_NO.STEP);
        if (returnStep > 999) {
            return String.format("%s%s", currentDate, returnStep);
        } else if (returnStep > 99) {
            return String.format("%s0%s", currentDate, returnStep);
        } else if (returnStep > 9) {
            return String.format("%s00%s", currentDate, returnStep);
        } else {
            return String.format("%s000%s", currentDate, returnStep);
        }
    }

    /**
     * 次季度首月的最后一天
     */
    public static Date firstMonthLastDay(){
        Date date = new Date();
        // 获取当前年
        int year  = DateUtil.year(date);
        // 获取当前月
        int i = DateUtil.month(date);
        // 计算当前季度
        i = (i-1)/3+1;
        // 次季度的数字，0为上次季度，1为下次季度
        int halfQuarter = i % 2;
        // 计算次季度开始的月份
        int halfQuarterStartMonth = (i - halfQuarter * 2) * 3 + 1;
        Calendar calendar = Calendar.getInstance();
        // 月份，注意月份从0开始计数，即1代表1月
        int month = halfQuarterStartMonth;
        // 设置为当月的第一天
        calendar.set(year, month, 1);
        // 获取当月的天数最后一天
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        String  monthStr = "";
        if(month<10){
            monthStr = "0"+month;
        }else{
            monthStr = String.valueOf(month);
        }
        String time = year+"-"+monthStr+"-"+lastDay;
        try{
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date format = dateFormat.parse(time);
            return format;
        }catch (Exception e){
            return null;
        }
    }
}
